package com.oauth.data;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import android.util.Log;

import com.oauth.utils.Base64Encoder;
import com.oauth.utils.ConfigUtil;
import com.oauth.utils.HttpClientUtil;
import com.oauth.utils.LogUtil;

public class OAuth {
	private Random random = new Random();
	private String encoding = "utf-8"; // URL编码方式
	private String oauth_request_method = "GET"; // 请求方法
	private String Base_url; // 请求url
	private String oauth_consumer_key; // —�? 注册应用后由应用服务商提�?
	private String consumer_secret; // —�? 注册应用后由应用服务商提�?
	private String oauth_token;
	private String oauth_token_secret;
	private String oauth_verifier; // 验证�?
	private String oauth_callback = ""; // —�? 用户授权后的返回地址
	private String oauth_nonce; // —�? 随机字符串，须保证每次都不同 �?2个字符长度）
	private String oauth_timestamp; // —�? 时间�?
	private String oauth_signature_method = "HMAC-SHA1"; // —�? 签名base string
															// 的方法，目前支持
	// HMAC-SHA1
	private String oauth_version = "1.0"; // —�? Oauth协议版本
	private String oauth_signature; // oauth请求�?�?

	private final String TAG = "OAuth";

	private static OAuth instance;

	public void setOauthTokenAndOauthTokenSercet(String oauth_token,
			String oauth_token_secret, String oauth_verifier) {
		this.oauth_token = oauth_token;
		this.oauth_token_secret = oauth_token_secret;
		this.oauth_verifier = oauth_verifier;
	}

	/**
	 * 初始化oauth_token、oauth_token_secret、oauth_verifier
	 */
	public void clear() {
		this.oauth_token = null;
		this.oauth_token_secret = null;
		this.oauth_verifier = null;
	}

	public static synchronized OAuth getInstance() {
		if (instance == null) {
			instance = new OAuth();
		}
		return instance;
	}

	public OAuth() {

	}

	public void setKeyAndSecret(String key, String secret) {
		this.oauth_consumer_key = key;
		this.consumer_secret = secret;
	}

	/**
	 * 拼接请求的url
	 * 
	 * @return Query String
	 * @throws Exception
	 */
	public String getRequestUrl() throws Exception {
		this.oauth_nonce = getAauth_nonce();
		this.oauth_timestamp = getAauth_timestamp();
		String baseUrlString = this.getBaseString();
		this.oauth_signature = this.getAauth_signature(baseUrlString);
		return Base_url + "?" + baseUrlString + "&oauth_signature="
				+ URLEncoder.encode(oauth_signature, encoding);
	}

	/**
	 * 获取随机�?oauth_nonce
	 * 
	 * @return
	 */
	private String getAauth_nonce() {
		return String.valueOf(random.nextInt(9876599) + 123400);
	}

	/**
	 * 获取时间�?
	 * 
	 * @return
	 */
	private String getAauth_timestamp() {
		return String.valueOf(System.currentTimeMillis()).substring(0, 10);
		// return String.valueOf(System.currentTimeMillis() / 1000);
	}

	/**
	 * 拼接�?��参数
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	private String getBaseString() throws UnsupportedEncodingException {

		String bsss = "";
		String curWeibo = ConfigUtil.getInstance().getCurWeibo();

		if ((oauth_verifier == null || oauth_verifier.length() == 0)) {
			bsss += "oauth_callback="
					+ URLEncoder.encode(ConfigUtil.callBackUrl, encoding) + "&";
		}
		bsss += "oauth_consumer_key="
				+ URLEncoder.encode(oauth_consumer_key, encoding);
		bsss += "&oauth_nonce=" + URLEncoder.encode(oauth_nonce, encoding);
		bsss += "&oauth_signature_method="
				+ URLEncoder.encode(oauth_signature_method, encoding);
		bsss += "&oauth_timestamp="
				+ URLEncoder.encode(oauth_timestamp, encoding);

		if (oauth_token != null && (oauth_token.length() > 0)) {
			bsss += "&oauth_token=" + URLEncoder.encode(oauth_token, encoding);
		}
		if (oauth_verifier != null && oauth_verifier.length() > 0) {
			bsss += "&oauth_verifier="
					+ URLEncoder.encode(oauth_verifier, encoding);
		}

		bsss += "&oauth_version=" + URLEncoder.encode(oauth_version, encoding);

		return bsss;
	}

	/**
	 * 获取 oauth_signature
	 * 
	 * @param data
	 * @param key
	 * @return
	 */
	public String getAauth_signature(String data) throws Exception {
		byte[] byteHMAC = null;
		try {
			String bss = oauth_request_method + "&"
					+ URLEncoder.encode(Base_url, encoding) + "&";
			String bsss = URLEncoder.encode(data, encoding);
			String urlString = bss + bsss;

			String oauthKey = URLEncoder.encode(consumer_secret, encoding)
					+ "&"
					+ ((oauth_token_secret == null || oauth_token_secret
							.equals("")) ? "" : URLEncoder.encode(
							oauth_token_secret, encoding));
			Mac mac = Mac.getInstance("HmacSHA1");
			SecretKeySpec spec = new SecretKeySpec(
					oauthKey.getBytes("US-ASCII"), "HmacSHA1");
			mac.init(spec);
			byteHMAC = mac.doFinal((urlString).getBytes("US-ASCII"));
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException ignore) {

		}

		String oauthSignature = new Base64Encoder().encode(byteHMAC);
		return oauthSignature;
	}

	/**
	 * 获取 oauth_token
	 * 
	 * @return
	 */
	private String getOauthToken() {
		String oauthToken = null;
		this.Base_url = ConfigUtil.getInstance().getRequest_token_url();

		try {

			String url = this.getRequestUrl();
			Log.i(TAG, "url--->" + url);
			LogUtil.d(OAuth.this, "获取未经授权的token requst_token_url = " + url);

			oauthToken = HttpClientUtil.get(url, "utf-8");

			LogUtil.d(OAuth.this, "未经授权的oauthToken = " + oauthToken);
		} catch (Exception e) {
			Log.i(TAG, "-----getOauthToken----Exception-----");
			e.printStackTrace();
		}

		return oauthToken;
	}

	/**
	 * 获取AccessToken
	 * 
	 * @return
	 */
	public String getAccessToken() {
		String oauthToken = null;
		this.Base_url = ConfigUtil.getInstance().getAccess_token_url();

		try {
			String url = this.getRequestUrl();
			LogUtil.d(OAuth.this, "取得授权成功的请�?= " + getRequestUrl());

			oauthToken = HttpClientUtil.get(url, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("getAccessToken() Exception");
		}

		return oauthToken;
	}

	public void setOauth_callback(String oauthCallback) {
		oauth_callback = oauthCallback;
	}

	/**
	 * 获取授权URL 用户登录页面地址
	 * 
	 * @return 授权URL
	 */
	public String getAuthorizUrl() {

		String oauthToken = this.getOauthToken();

		try {
			String[] oauth = oauthToken.split("&");
			String[] oauthTokenTmp = oauth[0].split("=");
			oauth_token = oauthTokenTmp[1];
			String tmp = oauth[1];
			String[] tmps = tmp.split("=");
			oauth_token_secret = tmps[1];
		} catch (Exception e) {
			e.printStackTrace();
		}

		String token = "oauth_token=" + oauth_token;
		String url = ConfigUtil.getInstance().getAuthoriz_token_url() + "?"
				+ token;
		String curWeibo = ConfigUtil.getInstance().getCurWeibo();

		LogUtil.d(OAuth.this, "请求授权页面的url[] = " + url);
		return url;
	}

	/**
	 * 设置验证�?
	 * 
	 * @param oauthVerifier
	 *            验证�?
	 */
	public void setOauthVerifier(String oauthVerifier) {
		this.oauth_verifier = oauthVerifier;
	}

	public String getOauth_token() {
		return oauth_token;
	}

	public void setOauth_token(String oauthToken) {
		oauth_token = oauthToken;
	}

	public String getOauth_token_secret() {
		return oauth_token_secret;
	}

	public void setOauth_token_secret(String oauthTokenSecret) {
		oauth_token_secret = oauthTokenSecret;
	}

}
